package com.cy.pj.sys.service.Impl;

import com.cy.pj.common.exception.ServiceException;
import com.cy.pj.common.exception.UpdateException;
import com.cy.pj.sys.dao.SysNoticeDao;
import com.cy.pj.sys.pojo.SysNotice;
import com.cy.pj.sys.service.SysNoticeService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 公告业务的具体实现
 * 1)核心业务
 * 2)拓展业务(日志纪录,权限控制,事务控制,缓存,.....)
 */
@Service
public class SysNoticeServiceImpl  implements SysNoticeService {
   //初始化日志对象(org.slf4j.*)
    private static final Logger log = LoggerFactory.getLogger(SysNoticeServiceImpl.class);

   //has a
    @Autowired
    //假如此位置没有写这个注解,则可以通过构造方法的形式为属性赋值
    private SysNoticeDao sysNoticeDao;
    //通过构造方法直接进行DI操作
    //public SysNoticeServiceImpl(SysNoticeDao sysNoticeDao){
       // this.sysNoticeDao = sysNoticeDao;
    //}

    @Override
    public List<SysNotice> findNotices(SysNotice sysNotice) {
        long t1 = System.currentTimeMillis();
        log.info("method start {}",t1);//{}为占位符,把t1传进{}
        List<SysNotice> list = sysNoticeDao.selectNotices(sysNotice);
        if (list.isEmpty()){
            throw new ServiceException("查询数据为空");
        }
        //System.out.println("list="+list.toString());
        long t2 = System.currentTimeMillis();
        log.info("method end {}",t2);
        return list;
    }

    @Override
    public int insertNotice(SysNotice sysNotice) {
        int i = sysNoticeDao.insertNotice(sysNotice);
        return i;
    }



    @Override
    public int updateNotice(SysNotice sysNotice) {

        int i = sysNoticeDao.updateNotice(sysNotice);
        if (i==0){
            throw new UpdateException("异常处理啦!");


        }
        return i;
    }

    @Override
    public int deleteById(Long[] ids) {
        int rows = sysNoticeDao.deleteById(ids);
        if (rows==0)
            //throw new RuntimeException("纪录可能已经不存在了");
            throw  new ServiceException("纪录可能已经不存在了");


        return rows;
    }

    @Override
    public SysNotice selectOne(Long id) {

        long t1 = System.currentTimeMillis();
        log.info("method start {}",t1);
        SysNotice notice = sysNoticeDao.selectById(id);
        if (notice==null){
            throw  new ServiceException("纪录可能已经不存在了");
        }
        long t2 = System.currentTimeMillis();
        log.info("method end {}",t2);

        return notice;
    }
}
